package io.bdmc.modules.bss.controller;

import java.time.LocalDateTime;

import io.bdmc.modules.bss.entity.DeviceDevice;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceAlarmMask;
import io.bdmc.modules.bss.model.DeviceAlarmMaskParam;
import io.bdmc.modules.bss.service.IDeviceAlarmMaskService;
import io.bdmc.modules.sys.controller.AbstractController;

/**
 * <p>
 * 报警屏蔽 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2021-06-25
 */

// "报警屏蔽", description = "报警屏蔽")
@RestController
@RequestMapping("/bss/alarm/mask")
public class DeviceAlarmMaskController extends AbstractController {

    @Autowired
    IDeviceAlarmMaskService _maskService;

    @Autowired
    IDeviceDeviceService _deviceService;

    @GetMapping("/list")
    // "分页查询报警信息", notes = "分页查询报警信息")
    public R list(DeviceAlarmMaskParam params) {
        MyPage<DeviceAlarmMask> myPage = new MyPage<>(params.getCurrent(), params.getSize());
        MyPage<DeviceAlarmMask> map = _maskService.mySelectPage(myPage, params);
        return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));
    }

    @GetMapping("/info")
    // "查询链路", notes = "查询链路")
    public R list(int maskId) {
        final DeviceAlarmMask mask = _maskService.getById(maskId);
        return R.ok().putData(mask);
    }

    @SysLogger("新增报警屏蔽")
    @PostMapping("/add")
    // "新增报警屏蔽", notes = "新增报警屏蔽")
    public R add(@RequestBody DeviceAlarmMask deviceAlarmMask) {
        boolean b;
        try {
            deviceAlarmMask.setCreateTime(LocalDateTime.now());
            deviceAlarmMask.setDeptId(getDeptId().intValue());
            DeviceDevice device = _deviceService.getById(deviceAlarmMask.getDeviceId());
            deviceAlarmMask.setDeviceSn(device.getDeviceSn());
            b = deviceAlarmMask.insert();
        } catch (Exception e) {
            if (e instanceof PersistenceException) {
                return R.error("已存在的屏蔽设置，请勿重复添加");
            } else {
                return R.error(e.getMessage());
            }
        }
        return R.trueOrFalse(b);
    }

    @SysLogger("修改报警屏蔽")
    @PostMapping("/update")
    // "修改报警屏蔽", notes = "修改报警屏蔽")
    public R update(@RequestBody DeviceAlarmMask deviceAlarmMask) {
        boolean b;
        try {
            b = deviceAlarmMask.updateById();
        } catch (Exception e) {
            if (e instanceof PersistenceException) {
                return R.error("已存在的屏蔽设置，请勿重复添加");
            } else {
                return R.error(e.getMessage());
            }
        }
        return R.trueOrFalse(b);
    }

    @SysLogger("删除报警屏蔽")
    @PostMapping("/delete")
    // "删除报警屏蔽", notes = "删除报警屏蔽")
    public R delete(@RequestBody Integer maskId) {
        if (maskId > 0)
            _maskService.removeById(maskId);
        return R.ok();
    }
}